package cn.com.dhcc.turbo.service.exch;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.util.HttpClientUtil;
import cn.com.dhcc.app.core.util.Status;
import cn.com.dhcc.app.core.util.XstreamUtil;
import cn.com.dhcc.turbo.entity.api.Exch;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.service.registry.NodeInfoService;
import cn.com.dhcc.turbo.service.registry.vo.PipeLineFtp;

/**
 * 分布式节点信息获取
 * 
 * @日期 2015-5-25 上午9:52:56
 * @作者  高强
 */
@Service
public class DistributedNodeInfoService {
	private static String suffix="/api/exch.do";
	
	/**
	 * 根据节点编码获取同步信息地址
	 * @param nodeCode
	 * @return
	 */
	public String getExchUrl(String nodeCode)throws ServiceException{
		NodeInfo nodeInfo=nodeInfoService.getNodeByNodeCode(nodeCode);
		String intall=nodeInfo.getInstallUrl();
		String url=intall+suffix;
		return url;
	}
	/**
	 * 根据注册地址获取同步信息地址
	 * @param nodeCode
	 * @return
	 */
	public static String getRegistSyncUrl(String installUrl)throws ServiceException{
		String url=installUrl+suffix;
		return url;
	}
	@Autowired
	NodeInfoService nodeInfoService;

	/**
	 * 获取分布式的ftp信息
	 * @param nodeCode
	 * @return
	 * @throws ServiceException
	 */
	public PipeLineFtp getFtpPipeLineByNodeCode(String nodeCode) throws ServiceException{
		NodeInfo nodeInfo=nodeInfoService.getNodeByNodeCode(nodeCode);
		String intall=nodeInfo.getInstallUrl();
		String url=intall+suffix;
		Exch exch=new Exch();
		
		//获取节点的ip，端口号
		exch.setActionName("getNodeFtpInfo");
		HashMap<String, String> paramMap=new HashMap<String, String>();
		
		try {
			paramMap.put("exch", XstreamUtil.toXml(exch));
		} catch (Exception e) {
			throw new ServiceException("获取分布式ftp信息失败:"+e.getMessage());
		}
		
		Status status=HttpClientUtil.post(url, paramMap);
		if (status.isSuccess()) {
			String[] strs=status.getData().toString().split("\\|");
			if (strs.length>=2) {
				try {
					System.out.println("获取分布式ftp信息成功:返回的信息["+status.getData()+"],exch:["+exch+"],请求的参数exchXML:["+XstreamUtil.toXml(exch)+"]");
				} catch (Exception e) {
					throw new ServiceException(e.getMessage());
				}
				PipeLineFtp ftp=new PipeLineFtp();
				ftp.setIp(strs[0]);
				ftp.setPort(Integer.parseInt(strs[1]));
				return ftp;
			}else{
				try {
					throw new ServiceException("获取节点["+nodeCode+"]分布式ftp信息失败,请求地址["+url+"]:返回的信息["+status.getData()+"]不合法,exch:["+exch+"],请求的参数exchXML:["+XstreamUtil.toXml(exch)+"]");
				} catch (Exception e) {
					throw new ServiceException(e.getMessage());
				}
			}
		}else{
			throw new ServiceException("获取节点["+nodeCode+"]分布式ftp信息失败,请求地址["+url+"]:"+status.getMsg());
		}
	}
	
	/**
	 * 获取分布式的ftp信息
	 * @param nodeCode
	 * @return
	 * @throws ServiceException
	 */
	public String getRmi(NodeInfo nodeInfo) throws ServiceException{
		String intall=nodeInfo.getInstallUrl();
		String url=intall+suffix;
		Exch exch=new Exch();
		
		//获取同步的rmi信息
		exch.setActionName("getRmiInfo");
		HashMap<String, String> paramMap=new HashMap<String, String>();
		try {
			paramMap.put("exch", XstreamUtil.toXml(exch));
		} catch (Exception e) {
			throw new ServiceException("获取["+nodeInfo.getNodeCode()+"]分布式rmi信息失败:"+e.getMessage());
		}
		Status status=HttpClientUtil.post(url, paramMap);
		if (status.isSuccess()) {
			return status.getData().toString();
		}else{
			throw new ServiceException("获取["+nodeInfo.getNodeCode()+"]分布式rmi信息失败:"+status.getMsg());
		}
	}
	/**
	 * 获取目的节点编码
	 * @param nodeInfo
	 * @return
	 * @throws ServiceException
	 */
	public String getNodeCode(String url ) throws ServiceException{
		url=url+suffix;
//		NodeInfo localNode = nodeInfoService.getLocalNode();
		Exch exch=new Exch();
		//获取目的节点编码
		exch.setActionName("getNodeCode");
		HashMap<String, String> paramMap=new HashMap<String, String>();
		try {
			paramMap.put("exch", XstreamUtil.toXml(exch));
		} catch (Exception e) {
			throw new ServiceException("获取目的节点编码信息失败:"+e.getMessage());
		}
		Status status=HttpClientUtil.post(url, paramMap);
		if (status.isSuccess()) {
			return status.getData().toString();
		}else{
			return "";
//			throw new ServiceException("获取分布式rmi信息失败:"+status.getMsg());
		}
	}
//	public static void main(String[] args) {
//		String str="10.1.201.4|2122";
//		System.out.println(str.split("\\|")[0]);
//		System.out.println("--------------");
//	}
}
